package com.electric.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.plugins.Page;
import com.electric.common.CommonStringUtils;
import com.electric.common.Contains;
import com.electric.common.LoggerInterface;
import com.electric.entity.Customer;
import com.electric.service.ICustomerService;
import com.electric.service.IRepairRecordService;

/**
 * <p>
 * 客户 前端控制器
 * </p>
 *
 * @author zhangbin
 * @since 2018-08-28
 */
@Controller
@RequestMapping("/customer")
public class CustomerController implements LoggerInterface {
	@Autowired
	private ICustomerService customerService;
	@Autowired
	private IRepairRecordService repairRecordService;

	@RequestMapping("/init")
	public ModelAndView init() {
		ModelAndView view = list(1, 0, null);
		view.setViewName("/customer/init");
		return view;
	}

	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(defaultValue = "1", name = "pageNum") int pageNum,
			@RequestParam(defaultValue = "0") int pageSize, String keyword) {
		ModelAndView view = new ModelAndView("/customer/contentTBody");

		if (pageSize == 0) {
			pageSize = Contains.PAGE_SIZE;
		}

		Page<Customer> page = customerService.selectPage(pageNum, pageSize, keyword);

		view.addObject("page", page);

		logger.debug(page);

		return view;
	}

	@RequestMapping("/moveList")
	public ModelAndView moveList(@RequestParam(defaultValue = "1", name = "pageNum") int pageNum,
			@RequestParam(defaultValue = "0") int pageSize,
			@RequestParam(name = "oldSeqId", required = true) String oldSeqId, String keyword) {
		if (pageSize == 0) {
			pageSize = Contains.MOVE_PAGE_SIZE;
		}

		ModelAndView view = list(pageNum, pageSize, keyword);

		view.setViewName("/customer/moveListTable");

		view.addObject("oldSeqId", oldSeqId);
		view.addObject("keyword", keyword);

		return view;
	}

	@RequestMapping("/preview")
	public ModelAndView preview(String seqId) {
		ModelAndView view = new ModelAndView("/customer/form");

		Customer customer = customerService.selectById(seqId);

		view.addObject("customer", customer);

		return view;
	}

	@RequestMapping("/detail")
	public ModelAndView detail(String seqId) {
		ModelAndView view = new ModelAndView("/customer/form");
		if (StringUtils.isNotBlank(seqId)) {
			Customer customer = customerService.selectById(seqId);

			view.addObject("customer", customer);
		}

		return view;
	}

	@RequestMapping("/add")
	@ResponseBody
	public String insert(String customerNumber, String customerName, String address) {
		String uuid = CommonStringUtils.getUUID();

		Customer customer = new Customer();
		customer.setSeqId(uuid);
		customer.setCustomerName(customerName);
		customer.setCustomerNumber(customerNumber);
		customer.setAddress(address);

		String check = validate(customer);
		if ("success".equals(check)) {
			try {
				logger.debug(customer);
				customerService.insert(customer);
			} catch (Exception e) {
				check = "DB_ER";
				logger.error(e.getMessage(), e);
			}
		}
		return check;
	}

	@RequestMapping("/update")
	@ResponseBody
	public String modify(String seqId, String customerNumber, String customerName, String address) {
		Customer customer = new Customer();
		customer.setSeqId(seqId);
		customer.setCustomerName(customerName);
		customer.setCustomerNumber(customerNumber);
		customer.setAddress(address);

		String check = validate(customer);
		if ("success".equals(check)) {
			try {
				logger.debug(customer);
				customerService.updateById(customer);
			} catch (Exception e) {
				check = "DB_ER";
				logger.error(e.getMessage(), e);
			}
		}
		return check;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public String delete(String seqId) {
		try {
			if (repairRecordService.checkExistsByCustomer(seqId)) {
				return "RR_ES";
			} else {
				customerService.deleteById(seqId);
				return "success";
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return "DB_ER";
		}
	}

	@RequestMapping("/cleanData")
	@ResponseBody
	public String cleanData(String seqId) {
		try {
			customerService.deleteById(seqId);
			repairRecordService.cleanDataByCustomer(seqId);
			return "success";
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return "DB_ER";
		}
	}

	@RequestMapping("/moveData")
	@ResponseBody
	public String moveData(String seqId, String newSeqId) {
		try {
			repairRecordService.moveDataByCustomer(seqId, newSeqId);
			return "success";
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return "DB_ER";
		}
	}

	private String validate(Customer customer) {
		if (StringUtils.isBlank(customer.getCustomerName())) {
			return "CN_NP";
		}
		if (StringUtils.isBlank(customer.getCustomerNumber())) {
			return "CM_NP";
		} else if (customerService.checkCustomerNumber(customer.getCustomerNumber(), customer.getSeqId())) {
			return "CM_ES";
		}
		if (StringUtils.isBlank(customer.getAddress())) {
			return "AD_NP";
		}
		if (StringUtils.isBlank(customer.getSeqId())) {
			return "ID_NP";
		}
		return "success";
	}
}
